package Wenyilin;

import java.util.*;

public class DoubleColorball {
    // 生成中奖号码
    public static int[] generateWinningNumbers() {
        int[] numbers = new int[7];
        // 生成6个不重复的红球号码
        Random random = new Random();
        Set<Integer> redSet = new HashSet<>();
        while (redSet.size() < 6) {
            redSet.add(random.nextInt(33) + 1);
        }
        int index = 0;
        for (int num : redSet) {
            numbers[index++] = num;
        }
        // 生成蓝球号码
        numbers[6] = random.nextInt(16) + 1;
        return numbers;
    }

    // 获取用户输入的号码
    public static int[] getUserNumbers() {
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[7];
        // 输入6个不重复的红球号码
        Set<Integer> redSet = new HashSet<>();
        System.out.println("请输入6个不重复的红球号码（1-33）：");
        for (int i = 0; i < 6; i++) {
            while (true) {
                int num = scanner.nextInt();
                if (num < 1 || num > 33) {
                    System.out.println("号码超出范围，请重新输入：");
                } else if (!redSet.add(num)) {
                    System.out.println("号码已存在，请重新输入：");
                } else {
                    numbers[i] = num;
                    break;
                }
            }
        }
        // 输入蓝球号码
        System.out.println("请输入1个蓝球号码（1-16）：");
        while (true) {
            int num = scanner.nextInt();
            if (num < 1 || num > 16) {
                System.out.println("号码超出范围，请重新输入：");
            } else {
                numbers[6] = num;
                break;
            }
        }
        return numbers;
    }

    // 对比号码并输出结果
    public static void compareNumbers(int[] winningNumbers, int[] userNumbers) {
        int redCount = 0;
        int blueCount = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (winningNumbers[i] == userNumbers[j]) {
                    redCount++;
                }
            }
        }
        if (winningNumbers[6] == userNumbers[6]) {
            blueCount++;
        }

        System.out.println("中奖号码：");
        for (int i = 0; i < 6; i++) {
            System.out.print(winningNumbers[i] + " ");
        }
        System.out.println("+" + winningNumbers[6]);

        System.out.println("您的投注号码：");
        for (int i = 0; i < 6; i++) {
            System.out.print(userNumbers[i] + " ");
        }
        System.out.println("+" + userNumbers[6]);

        System.out.println("命中红球数：" + redCount);
        System.out.println("命中蓝球数：" + blueCount);

        // 根据命中球数确定中奖级别
        if (redCount == 6 && blueCount == 1) {
            System.out.println("恭喜您，中了一等奖！");
        } else if (redCount == 6 && blueCount == 0) {
            System.out.println("恭喜您，中了二等奖！");
        } else if (redCount == 5 && blueCount == 1) {
            System.out.println("恭喜您，中了三等奖！");
        } else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)) {
            System.out.println("恭喜您，中了四等奖！");
        } else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)) {
            System.out.println("恭喜您，中了五等奖！");
        } else if ((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1) || (redCount == 0 && blueCount == 1)) {
            System.out.println("恭喜您，中了六等奖！");
        } else {
            System.out.println("很遗憾，未中奖。");
        }
    }

    public static void main(String[] args) {
        int[] winningNumbers = generateWinningNumbers();
        int[] userNumbers = getUserNumbers();
        compareNumbers(winningNumbers, userNumbers);
    }
}
